package com.demo.excel;

import cn.hutool.extra.spring.SpringUtil;
import com.demo.eneity.User;
import com.demo.mapper.UserMapper;
import com.demo.utils.AMapUtils;

import java.util.List;
import java.util.concurrent.CountDownLatch;


public class UserThread implements Runnable {

    private int startIndex;

    private int endIndex;

    private List<User> list;

    private CountDownLatch count;

    private UserMapper userMapper;

    public UserThread(int startIndex, int endIndex, List<User> list, CountDownLatch count) {
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.list = list;
        this.count = count;
    }

    @Override
    public void run() {
        try {
            List<User> newList = list.subList(startIndex, endIndex);
            for (User user : newList) {
                double[] doubles = AMapUtils.addressToGPS(user.getHotelAddress());
                if (doubles != null) {
                    user.setLatitude(String.valueOf(doubles[0]));
                    user.setLongitude(String.valueOf(doubles[1]));
                } else {
                    // Handle the case where coordinates cannot be obtained
                    user.setLatitude(null);
                    user.setLongitude(null);
                }
            }
            //防止空插入
            if (newList.size() > 0) {
                UserMapper userMapper = SpringUtil.getBean(UserMapper.class);
                userMapper.insertBatchSomeColumn(newList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //计数减一
            count.countDown();
        }
    }
}
